#!/bin/bash

echo "========================================"
echo "区块链证券交易所系统启动脚本"
echo "========================================"
echo ""

# 检查环境
echo "[1/5] 检查环境..."
if ! command -v node &> /dev/null; then
    echo "[错误] 未找到 Node.js，请先安装 Node.js"
    exit 1
fi

if ! command -v java &> /dev/null; then
    echo "[错误] 未找到 Java，请先安装 JDK 11+"
    exit 1
fi

if ! command -v mvn &> /dev/null; then
    echo "[错误] 未找到 Maven，请先安装 Maven"
    exit 1
fi

echo "[✓] 环境检查通过"
echo ""

# 启动 Hardhat 节点
echo "[2/5] 启动 Hardhat 本地节点..."
cd blockchain
if [ ! -d "node_modules" ]; then
    echo "首次运行，安装依赖..."
    npm install
fi

# 使用 tmux 或 screen（如果可用），否则使用后台进程
if command -v tmux &> /dev/null; then
    tmux new-session -d -s hardhat "npm run node"
    echo "[✓] Hardhat 节点在 tmux 会话中启动"
elif command -v screen &> /dev/null; then
    screen -dmS hardhat npm run node
    echo "[✓] Hardhat 节点在 screen 会话中启动"
else
    npm run node > hardhat.log 2>&1 &
    HARDHAT_PID=$!
    echo "[✓] Hardhat 节点启动中 (PID: $HARDHAT_PID)"
fi

sleep 5
echo ""

# 部署合约
echo "[3/5] 部署智能合约..."
npm run deploy
if [ $? -ne 0 ]; then
    echo "[错误] 合约部署失败"
    exit 1
fi
echo "[✓] 合约部署成功"
echo ""

# 启动后端
echo "[4/5] 启动后端服务..."
cd ../backend

if command -v tmux &> /dev/null; then
    tmux new-session -d -s backend "mvn spring-boot:run"
    echo "[✓] 后端服务在 tmux 会话中启动"
elif command -v screen &> /dev/null; then
    screen -dmS backend mvn spring-boot:run
    echo "[✓] 后端服务在 screen 会话中启动"
else
    mvn spring-boot:run > backend.log 2>&1 &
    BACKEND_PID=$!
    echo "[✓] 后端服务启动中 (PID: $BACKEND_PID)"
fi

sleep 10
echo ""

# 启动前端
echo "[5/5] 启动前端应用..."
cd ../frontend
if [ ! -d "node_modules" ]; then
    echo "首次运行，安装依赖..."
    npm install
fi

if command -v tmux &> /dev/null; then
    tmux new-session -d -s frontend "npm run dev"
    echo "[✓] 前端应用在 tmux 会话中启动"
elif command -v screen &> /dev/null; then
    screen -dmS frontend npm run dev
    echo "[✓] 前端应用在 screen 会话中启动"
else
    npm run dev > frontend.log 2>&1 &
    FRONTEND_PID=$!
    echo "[✓] 前端应用启动中 (PID: $FRONTEND_PID)"
fi

echo ""
echo "========================================"
echo "系统启动完成！"
echo "========================================"
echo ""
echo "请按以下步骤操作："
echo "1. 等待所有服务启动完成（约30秒）"
echo "2. 打开浏览器访问: http://localhost:5173"
echo "3. MetaMask 连接:"
echo "   - 系统会自动检测并提示切换网络"
echo "   - 点击"确定"即可自动配置"
echo "   - 手动配置: RPC URL: http://127.0.0.1:8545, Chain ID: 31337"
echo "4. 导入测试账户"
echo ""
if command -v tmux &> /dev/null; then
    echo "使用以下命令查看各服务状态："
    echo "  - Hardhat: tmux attach -t hardhat"
    echo "  - Backend: tmux attach -t backend"
    echo "  - Frontend: tmux attach -t frontend"
    echo ""
    echo "停止服务: tmux kill-session -t [session-name]"
elif command -v screen &> /dev/null; then
    echo "使用以下命令查看各服务状态："
    echo "  - Hardhat: screen -r hardhat"
    echo "  - Backend: screen -r backend"
    echo "  - Frontend: screen -r frontend"
    echo ""
    echo "停止服务: screen -X -S [session-name] quit"
else
    echo "进程 PID 已保存，使用 kill 命令停止服务"
fi
echo "========================================"


